package com.dayuanit.dy17.mix.httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestHttpClient {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

//        HttpGet httpGet = new HttpGet("http://127.0.0.1:8092/user/login.do?username=steve&password=111&code=555");
//        CloseableHttpResponse response = httpclient.execute(httpGet);
//
//        HttpEntity entity = response.getEntity();
//        String result = EntityUtils.toString(entity);
//        System.out.println(result);

        HttpPost httpPost = new HttpPost("http://127.0.0.1:8092/user/login.do");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "steve"));
        nvps.add(new BasicNameValuePair("password", "111"));
        nvps.add(new BasicNameValuePair("code", "5555"));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        CloseableHttpResponse response = httpclient.execute(httpPost);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);

    }
}
